package com.xiaolai.iodemo;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Base64;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.xiaolai.iodemo.clientcore.ClientSocketConnection;

import java.text.SimpleDateFormat;

public class MainActivity extends Activity {

    EditText textMsg = null;
    TextView textFeild = null;
    int port = 0;
    String ip = null;
    String alias=null;
    String DEVICEID=null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();

    }
    protected void init() {
        textMsg = (EditText) findViewById(R.id.editText_Msg);
        textFeild = (TextView) findViewById(R.id.textView_TextFiled);
        //取出数据
        Intent intent=getIntent();
        Bundle bundle=intent.getExtras();
        port=Integer.valueOf(bundle.getString("port"));
        ip =bundle.getString("ip");
        alias=bundle.getString("alias");
    }

    public void onclickRefresh(View v) {
        ClientSocketConnection conn = new ClientSocketConnection(this.getApplicationContext(),ip,port,alias);
        conn.createConn();
        //发送和刷新消息
        conn.sendMsg(textMsg.getText().toString());
        String temp=alias+" 在 "+ new SimpleDateFormat("HH:mm:ss").toString()+" 说: "+textMsg.getText().toString();
        textFeild.setText(textFeild.getText()+temp);
        conn.close();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main2, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
